from selenium import webdriver
from selenium.webdriver.chrome.service import Service

from common.ChromeDriverManager import ChromeDriverManager
from common.tools import get_driver_path


class DriverConfig:
    @staticmethod
    def driver_config():
        """
        浏览器驱动
        :return:
        """
        options = webdriver.ChromeOptions()
        # 设置窗口大小
        options.add_argument("window-size=1920,1080")
        # 无头模式
        # options.add_argument('--headless')
        # 解决卡顿
        options.add_argument("--disable-gpu")
        options.add_argument("--no-sandbox")
        options.add_argument('--disable-dev-shm-usage')
        # 解决selenium无法访问https的问题
        options.add_argument("--ignore-certificate-errors")
        options.add_argument("--allow-insecure-localhost")
        # 无痕模式
        # options.add_argument("--incognito")
        # 去除"chrome正受到自动测试软件的控制"的提示
        options.add_argument("disable-infobars")
        options.add_experimental_option(
            "excludeSwitches", ["enable-automation"]
        )
        options.add_experimental_option("prefs", {
            "credentials_enable_service": False,  # 禁用密码保存服务
            "profile.password_manager_enabled": False  # 禁用密码管理器
        })
        # 实例化浏览器驱动
        service = Service(get_driver_path("chromedriver.exe"))
        driver = webdriver.Chrome(options, service)
        # driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
        # 隐性等待时间
        driver.implicitly_wait(3)
        # 删除所有cookies
        driver.delete_all_cookies()
        return driver


if __name__ == "__main__":
    DriverConfig().driver_config()
